package com.acwing.partition12;

import java.io.*;
import java.util.PriorityQueue;

/**
 * @author `RKC`
 * @date 2022/1/11 22:29
 */
public class AC1135新年好 {

    private static final int N = 50010, M = (int) ((1e5 + 10) * 2), INF = 0x3f3f3f3f;
    private static int n = 0, m = 0, index = 0;

    //因为不知道亲戚节点的节点号，使用索引来联系起来
    private static final int[] relative = new int[6];
    private static boolean[] visited = new boolean[6];
    private static int[][] dist = new int[6][N];

    private static int[] head = new int[N], element = new int[M], next = new int[M], weight = new int[M];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        s = reader.readLine().split("\\s+");
        relative[0] = 1;
        for (int i = 1; i < relative.length; i++) relative[i] = Integer.parseInt(s[i - 1]);
        for (int i = 0; i < N; i++) head[i] = -1;
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
            createEdge(u, v, w);
            createEdge(v, u, w);
        }
        //预处理出来几个节点的最短路径
        for (int i = 0; i < relative.length; i++) dijkstra(relative[i], dist[i]);
        //从1号点开始走，dfs遍历完所有亲戚访问顺序，得到最优策略
        writer.write(dfs(1, 0, 0) + "\n");
        writer.flush();
    }

    private static int dfs(int count, int start, int dis) {
        if (count == 6) return dis;
        int ans = INF;
        for (int i = 1; i < relative.length; i++) {
            if (visited[i]) continue;
            visited[i] = true;
            ans = Math.min(ans, dfs(count + 1, i, dis + dist[start][relative[i]]));
            visited[i] = false;
        }
        return ans;
    }

    private static void dijkstra(int start, int[] dist) {
        for (int i = 0; i < N; i++) dist[i] = INF;
        dist[start] = 0;
        PriorityQueue<int[]> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
        heap.add(new int[]{start, 0});
        while (!heap.isEmpty()) {
            int[] pair = heap.poll();
            int u = pair[0];
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (dist[v] > dist[u] + weight[i]) {
                    dist[v] = dist[u] + weight[i];
                    heap.add(new int[]{v, dist[v]});
                }
            }
        }
    }

    private static void createEdge(int u, int v, int w) {
        element[index] = v;
        next[index] = head[u];
        head[u] = index;
        weight[index++] = w;
    }
}
